//
//  ViewController.m
//  23-HomeWorkQQMusic
//
//  Created by dllo on 16/8/12.
//  Copyright © 2016年 LST. All rights reserved.
//

#import "ViewController.h"
#import "MyCollectionViewCell.h"
#import "HeaderCollectionReusableView.h"
#import "AFNetworking.h"
#import "RootModel.h"
#import "BaseModel.h"
#import "MvList.h"
#import "List.h"
#import "Contents.h"
#import "Data.h"
#define WANGZHI @"http://i.y.qq.com/v8/fcg-bin/getmv_by_tag?cmd=mv_shows_new&g_tk=5381&uin=0&format=json&inCharset=utf-8&outCharset=utf-8&notice=0&platform=h5&needNewCode=1&_=1470996835294"
@interface ViewController ()<UICollectionViewDelegate, UICollectionViewDataSource>
@property (nonatomic, strong)NSMutableArray *headerArr;
@property (nonatomic, strong)RootModel *rootModel;
@property (nonatomic, strong)UICollectionView *collectionView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.headerArr = [NSMutableArray array];
    // 左按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(leftBtn:)];
    // 右按钮
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:self action:@selector(rightBtn:)];
    self.navigationController.navigationBar.barTintColor = [UIColor colorWithRed:0.18 green:0.79 blue:0.48 alpha:1.00];
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatHeaderBtn];
    [self creatCollectionView];
    [self handleData];
}
- (void)creatHeaderBtn {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(70, 5, 60, 30);
    [btn setTitle:@"我的" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [self.navigationController.navigationBar addSubview:btn];
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    btn1.frame = CGRectMake(130, 5, 70, 30);
    [btn1 setTitle:@"音乐馆" forState:UIControlStateNormal];
    [self.navigationController.navigationBar addSubview:btn1];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    btn2.frame = CGRectMake(200, 5, 60, 30);
    [btn2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn2 setTitle:@"发现" forState:UIControlStateNormal];
    [self.navigationController.navigationBar addSubview:btn2];
}
// collectionView
- (void)creatCollectionView {
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(self.view.bounds.size.width / 2 - 10, self.view.bounds.size.height / 4);
    layout.sectionInset = UIEdgeInsetsMake(5, 5, 25, 5);
    layout.minimumLineSpacing = 5;
    layout.minimumInteritemSpacing = 5;
    layout.headerReferenceSize = CGSizeMake(0, 100);
    _collectionView = [[UICollectionView alloc] initWithFrame:self.view.frame collectionViewLayout:layout];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
    _collectionView.backgroundColor = [UIColor whiteColor];
    layout.headerReferenceSize = CGSizeMake(0, self.view.bounds.size.width / 2);
    [self.view addSubview:_collectionView];
    [_collectionView registerClass:[HeaderCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"result"];
    [_collectionView registerClass:[MyCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
}
// 解析
- (void)handleData {
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    NSMutableSet *set = [NSMutableSet setWithSet:manager.responseSerializer.acceptableContentTypes];
    [set addObject:@"application/x-javascript"];
    manager.responseSerializer.acceptableContentTypes = set;
    [manager GET:WANGZHI parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSDictionary dictionaryWithDictionary:responseObject];
        self.rootModel = [RootModel modelWithDic:dic];
        [_collectionView reloadData];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    HeaderCollectionReusableView *headerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"result" forIndexPath:indexPath];
    List *list = _rootModel.data.contents.list[indexPath.section];
    headerView.list = list;
    return headerView;
   
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return _rootModel.data.contents.list.count;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return [_rootModel.data.contents.list[section] mvlist].count - 1;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.mvList = [_rootModel.data.contents.list[indexPath.section] mvlist][indexPath.row + 1];
    return  cell;
}


- (void)leftBtn:(UIBarButtonItem *)sender {
}
- (void)rightBtn:(UIBarButtonItem *)sender {
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
